Scroll Progress Bar

Destructor

In C++, a destructor is a special member function of a class that is automatically called when an object of the class goes out of scope, is explicitly deleted, or when the program terminates. The primary purpose of a destructor is to release any resources (such as memory) that were acquired by the object during its lifetime. Here are some key points about destructors in C++:

1. Destructor Syntax:
  • The destructor has the same name as the class preceded by a tilde (~).
  • Destructors do not take any parameters and do not have a return type.
  • There it can be only one destructor for a class.
Program:

class MyClass {
public:
    // Constructor
    MyClass() {
        // Constructor code
    }

    // Destructor
    ~MyClass() {
        // Destructor code (cleanup)
    }
};
2. Automatic Invocation:

Destructors are automatically invoked when an object of the class goes out of scope or when delete is called on a dynamically allocated object.

Program:

int main() {
    MyClass obj; // Object created, destructor called when 'obj' goes out of scope
    MyClass* ptr = new MyClass; // Dynamically allocated object, destructor called when 'delete ptr' is called
    // ...
    delete ptr; // Explicitly deleting the object, destructor is called
    return 0;
}
3. Resource Cleanup:

Destructors are commonly used to release resources like memory, file handles, or network connections. For objects that manage dynamically allocated memory, the destructor should deallocate the memory to prevent memory leaks.

Program:

class DynamicArray {
private:
    int* data;

public:
    // Constructor
    DynamicArray(int size) {
        data = new int[size];
    }

    // Destructor
    ~DynamicArray() {
        delete[] data; // Release dynamically allocated memory
    }
};
4. Implicitly Defined Destructor:

If a class does not provide a user-defined destructor, C++ provides an implicitly defined destructor. The implicit destructor does nothing for built-in data types but calls the destructors of member objects and base classes.

Program:

class NoDestructor {
public:
    // No user-defined destructor
};
5. Order of Destruction:

When an object contains other objects (composition) or is part of an inheritance hierarchy, destructors are called in reverse order of construction. Base class destructors are called before derived class destructors.

Program:

class Base {
public:
    ~Base() {
        std::cout << "Base destructor" << std::endl;
    }
};

class Derived : public Base {
public:
    ~Derived() {
        std::cout << "Derived destructor" << std::endl;
    }
};
6. Preventing Copy and Assignment:

To prevent copying and assignment of objects of a class, it can declare the copy constructor and copy assignment operator as private and leave them undefined.

Program:

class NonCopyable {
private:
    NonCopyable(const NonCopyable&);
    NonCopyable& operator=(const NonCopyable&);

public:
    // Constructor and other members
};

Destructors are essential for managing resources and preventing resource leaks in C++ programs. By releasing resources in a destructor, ensure that objects clean up after themselves when they are no longer needed, making code more robust and reliable.


question


answer

question2


answer2